﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using JSON;
/// <summary>
/// 主要启动程序
/// </summary>
public class BaseGameMain : ModuleComponent<BaseGameMain> {
	Loading_View _preLoadView;
	PreLoad _preLoad;
	public void Awake(){
		InitModuleName ();
		InitPreLoad();
		InitComponent ();
		FGameControl.getGameContorl().OnPreLoadBegin ();
		StartCoroutine (StartLoad ());
	}	
	/// <summary>
	/// 初始化模块名字 主要用来做 预加载之类的
	/// </summary>
	protected virtual void InitModuleName(){
		
	}
	/// <summary>
	/// 初始化生成MVC 各个组件
	/// </summary>
	protected virtual void InitComponent(){
	}
	/// <summary>
	/// 初始化预加载
	/// </summary>
	protected virtual void InitPreLoad(){
		_preLoadView = ViewUIMgr.Instance.ShowViewUI<Loading_View>(ViewUIPanelDepth.Top,"Base");
		_preLoadView.SetValue (0);
		_preLoadView._onClose += Finish;
		_preLoad = new PreLoad (BaseModel.Instance.ModueName);
	}
	IEnumerator  StartLoad(){
		if (_preLoad._dataCount == 0) {
			_preLoadView.SetValue (1);
			yield return null;
		} else {
			for (int i = 0; i < _preLoad._dataCount; i++) {
				_preLoad.Load (i);
				_preLoadView.SetValue (_preLoad._nowLoadValue);
				yield return null;
			}
		}

	}
	public void AddPreLoadData(string path,string name,bool isui){
		_preLoad.AddData (path,name,isui);
	}
	public void GatherPreLoadData(){
		_preLoad.RestCount ();
	}
	/// <summary>
	/// 预加载加载完毕
	/// </summary>
	protected virtual void Finish(){
		FGameControl.getGameContorl().OnPreLoadFinish ();
		GameObject.Destroy (gameObject);
	}
}
[System.Serializable]
public class PreLoad{
	public string _moduleName;
	public List<PreLoadData> _data;
	public int _dataCount;
	float _addValue = 0.01f;
	public float _nowLoadValue = 0;
	public PreLoad(string name){
		_moduleName = name;
		GatherData ();
	}
	void GatherData(){
		JSONNode json = JsonRead.Read ("PreLoad_" + _moduleName);
		_data = new List<PreLoadData> ();
		if (json != null) {
			for(int i = 0;i < json["Model"].Count;i++){
				_data.Add(new PreLoadData(json["Model"][i]));
			}
			for(int i = 0;i < json["Effect"].Count;i++){
				_data.Add(new PreLoadData(json["Effect"][i]));
			}
			for(int i = 0;i < json["UI"].Count;i++){
				_data.Add(new PreLoadData(json["UI"][i]));
			}
		}
		RestCount ();
	}
	public void AddData(string path,string name,bool isui){
		_data.Add (new PreLoadData (path, name, isui));
	}
	public void RestCount(){
		_dataCount = _data.Count;
		_addValue = 1f / _dataCount;
		_nowLoadValue = 0;
	}
	public void Load(int i){
		_data [i].Load ();
		_nowLoadValue += _addValue;
	}
}
[System.Serializable]
public class PreLoadData{
	public string _path;
	public string _name;
	public bool _isUI;
	public bool _isload;
	public PreLoadData(string path,string name,bool isui){
		_path = path;
		_name = name;
		_isUI = isui;
	}
	public PreLoadData(JSONNode json){
		_path = json["Path"].ToString();
		_isUI = json["UI"].AsInt != 0;
		_name = json ["Name"].ToString ();
	}
	public void Load(){
		_isload = true;
		Transform parent;
		if (_name == "null")
			return;
//		Debug.Log (_path + _name);
		parent = _isUI ? ObjManager.Instance.ParentUI : ObjManager.Instance.ParentDefualt;
		GameObject obj = ObjManager.Instance.AddChild (_path, _name, null, parent);
		ObjManager.Instance.Despawn (obj);
	}
}
